﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BreakOutBattles.IO;
#endregion

namespace BreakOutBattles.Game.UI
{
    /// <summary>
    /// Visualizes player statistics such as nickname, scoring and remaining lifes.
    /// </summary>
    public class Hud
    {
        #region Fields
        private const string PLAYER_NAME_HEADING = "Player: ";
        private const string PLAYER_SCORE_HEADING = "Score: ";
        private const string PLAYER_LIFE_HEADING = "Lifes: ";
        private const string GAME_OVER_MESSAGE = "GAME OVER";
        private const string LEVEL_DONE_MESSAGE = "LEVEL COMPLETED";

        private IOManager ioManager;
        private Player player;
        private Rectangle hudScreenRect;
        private Vector2 hudPosition;
        private Vector2 playerNamePos;
        private Vector2 playerScorePos;
        private Vector2 playerLifePos;
        private Vector2 gameMessagePos;
        private Vector2 summaryPos;
        private Vector2 summaryScore1;
        private Vector2 summaryScore2;
        private Vector2 summaryScore3;
        private Vector2 summaryTotalScore;

        private SpriteFont hudFont;
        private Texture2D hudTexture;
        private Texture2D summaryTexture;

        private bool showGameOverMessage = false;
        private bool showLevelCompletedMessage = false;
        private bool showScoreTable = false;
        #endregion

        /// <summary>
        /// Creates a new HUD.
        /// </summary>
        /// <param name="player">The player to be represented by this HUD.</param>
        /// <param name="hudScreenRect">
        /// The area of the current game screen that the HUD can use to
        /// render its contents.
        /// </param>
        public Hud(Player player, Rectangle hudScreenRect)
        {
            this.player = player;
            this.hudScreenRect = hudScreenRect;
            ioManager = IOManager.Instance;

            LoadContent();
            Initialize();
        }

        #region Initialization
        /// <summary>
        /// Loads the contents to be used by the HUD.
        /// </summary>
        private void LoadContent()
        {
            summaryTexture = ioManager.LoadTexture(ContentFolders.HUD, "summary");
            hudTexture = ioManager.LoadTexture(ContentFolders.HUD, "hud");
            hudFont = ioManager.LoadFont("hudFont");
        }

        /// <summary>
        /// Initializes the components of this hud.
        /// </summary>
        private void Initialize()
        {
            hudPosition = new Vector2(hudScreenRect.X, hudScreenRect.Y);

            float playerNameWidth = hudFont.MeasureString(player.Name).X;
            playerNamePos = new Vector2(hudScreenRect.Center.X - (playerNameWidth / 2), hudScreenRect.Top + 50);
            playerScorePos = new Vector2(hudScreenRect.Left + 180, hudScreenRect.Top + 83);
            playerLifePos = new Vector2(hudScreenRect.Right - 130, hudScreenRect.Top + 83);

            gameMessagePos = new Vector2(hudScreenRect.Center.X - 50, hudScreenRect.Top + 400);

            summaryPos = new Vector2(hudScreenRect.Center.X - summaryTexture.Width / 2, hudScreenRect.Center.Y - summaryTexture.Height / 2);
            summaryScore1 = new Vector2(summaryPos.X + 200, summaryPos.Y + 120);
            summaryScore2 = new Vector2(summaryPos.X + 200, summaryPos.Y + 180);
            summaryScore3 = new Vector2(summaryPos.X + 200, summaryPos.Y + 240);
            summaryTotalScore = new Vector2(summaryPos.X + 200, summaryPos.Y + 330);
        }
        #endregion

        /// <summary>
        /// Resets the HUD to its default properties.
        /// </summary>
        public void Reset()
        {
            showScoreTable = false;
        }

        public bool ShowScoreTable
        {
            get { return showScoreTable; }
            set { showScoreTable = value; }
        }

        /// <summary>
        /// Updates the HUD.
        /// </summary>
        public void Update(GameTime gameTime)
        { }

        private void DrawGameSummary(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(summaryTexture, summaryPos, Color.White);
            spriteBatch.DrawString(hudFont, player.Score + string.Empty, summaryScore1, Color.White);
            spriteBatch.DrawString(hudFont, player.Lifes + "  x200", summaryScore2, Color.White);
            spriteBatch.DrawString(hudFont, "0  x300", summaryScore3, Color.White);
            spriteBatch.DrawString(hudFont, player.TotalScore + string.Empty, summaryTotalScore, Color.White);
        }

        /// <summary>
        /// Renders the visual components of the HUD.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(hudTexture, hudPosition, Color.White);

            spriteBatch.DrawString(hudFont, player.Name, playerNamePos, Color.White);
            spriteBatch.DrawString(hudFont, player.Score + string.Empty, playerScorePos, Color.Yellow);
            spriteBatch.DrawString(hudFont, player.Lifes + string.Empty, playerLifePos, Color.Yellow);

            if (showScoreTable)
            {
                DrawGameSummary(spriteBatch);
            }
        }
    }
}
